<template>
	<view>
		<term-select :vm="termSelectVM" @selectChange="selectChange"></term-select>
		<baseForm :column.sync="column" @submitFun="submitData" :submitStatus="submitDisable" :button="button"></baseForm>
	</view>
</template>

<script>
import baseForm from '@/wxcomponents/form/form.vue';
import pickerModule from '@/wxcomponents/form/picker.vue';
import common from '@/libs/login.js';
import termSelect from '@/wxcomponents/termSelect/index.vue'

import { format, dictCode } from '@/libs/common.js';
import dictMixins from '@/libs/dictMixins.js'
export default {
	mixins: [dictMixins],
	data() {
		let that = this;
		return {
			button: {
				hide: false,
				text: '提交'
			},
			termSelectVM: {
				selectItem: '',
				placeHolder: '请选择学期',
				choices: [],
			},
			assessDetailList: [],
			isSubmit: '',
			termId: '',
			choices: []
		};
	},
	async onShow() {
		this.choices = this.findOneDictList('dict_teach_assess_type');
		await this.fetchTerm();
		this.EventBus.on('radioChange', obj => {
			let findRow = this.column.find(row => {
				return row.label == obj.key
			});
			if (findRow) {
				findRow.value = obj.value;
			}
		});
	},
	methods: {
		async fetchTerm() {
			let resp = await this.$http.get({
				url: '/edu/term/rpc/list'
			});
			let list = resp.result.map(item => {
				item.name = item.termName
				return item;
			})
			this.termSelectVM.choices = list;
			return list;
		},
		async selectChange(selectItem) {
			this.fetchData(selectItem.id)
		},
		async fetchData(termId) {
			if (!termId) {
				termId = this.termId;
			}
			let resp = await this.$http.post({
				url: `/edu/assess/wechat/detail/${termId}`,
			});
			Object.assign(this, resp.result);
		},
		async submitData() {
			let assessDetailList = this.column.map(col => {
				let obj = {
					assessType: col.value,
					// isSubmit: col.isSubmit,
					jobNumber: col.jobNumber,
					studentNumber: col.studentNumber,
					teacherName: col.teacherName,
					termId: col.termId,
				}
				return obj;
			})
			let req = {
				assessDetailList,
				termId: this.termId
			}
			let resp = await this.$http.post({
				url: `/edu/assess/wechat/save`,
				data: req,
				header: {
					'content-type': 'application/json;charset=UTF-8'
				}
			});
			let self = this;
			uni.showToast({
				title: '操作成功',
				icon:'none',
				duration: 500,
				complete() {
					self.fetchData();
				}
			})
		}
	},
	components: {
		baseForm,
		termSelect
	},
	computed: {
		submitDisable() {
			this.button.text = this.isSubmit ? '已提交' : '提交';
			return this.isSubmit;
		},
		column:{
			get() {
				this.button.hide = this.assessDetailList.length == 0
				let column = this.assessDetailList.map(row => {
					let obj = {
						label: row.teacherName,
						type: 'radio',
						disabled: this.submitDisable,
						value: row.assessType,
						showIndex: true,
						items: this.choices,
						...row
					}
					return obj;
				})
				return column;
			},
			set() {
				
			}
		}
	},
	watch: {
		
	}
};
</script>

<style lang="scss" scoped>
.user {
	text {
		font-size: 36upx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: rgba(51, 51, 51, 1);
		display: inline-block;
		margin-right: 20upx;
	}

	padding: 50upx;
}
</style>
